package com.yanjiali.controller;

import com.yanjiali.enumerate.LoginTypeEnum;
import com.yanjiali.model.R;
import com.yanjiali.model.req.LoginReq;
import com.yanjiali.model.resp.LoginResp;
import com.yanjiali.service.SysUserService;
import com.yanjiali.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Package: com.yanjiali.controller
 * @Author: yanjiali
 * @Created: 2025/4/12 16:32
 */
@RestController
@Api(tags = "登录控制器")
@RequestMapping("/v1")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;  //系统用户，管理员

    @Autowired
    private UserService userService;    //普通用户

    @PostMapping("/login")
    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginReq", value = "登录请求参数")
    })
    public R<LoginResp> login(@RequestBody LoginReq loginReq) {
        String loginType = loginReq.getLoginType().trim();
        if (loginType.equals(LoginTypeEnum.ADMIN_TYPE.getLoginType())) {
            return sysUserService.login(loginReq.getUsername(), loginReq.getPassword());
        }else if(loginType.equals(LoginTypeEnum.MEMBER_TYPE.getLoginType())) {
            return userService.login(loginReq.getUsername(), loginReq.getPassword());
        }else {
            return R.fail("未知的登录类型");
        }
    }
}
